//
//  WPMineTableViewController.m
//  wobingwoyi
//
//  Created by WGP on 16/7/21.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPMineViewController.h"
#import "WPMineScrollView.h"
#import "WPMineSecondView.h"
#import "WPMineThirdView.h"
#import "WPMineLoginVC.h"
#import "WPIndivalInfoVC.h"
#import "WPIndivalInfoNVC.h"
#import "CustomPresentAnimationController.h"
#import "WPSettingViewController.h"
#import "WPGoldCoinVC.h"
#import "WPMineInviteFriendsVC.h"
#import "WPMineFeedbackVC.h"
#import "WPHeaderView.h"
#import "WPMedicalRecordsVC.h"
#import "WPMinePrescriptionRecordsVC.h"
#import "WPMineInforamationVC.h"
#import "WPMineInfomationNVC.h"
#import "UIButton+WebCache.h"
#import "WPAboutUsVC.h"
#import "WPMineMyCollectionRecordVC.h"
#import "WPYBViewController.h"
#import "WPPhysicalExaminationFileVC.h"

@interface WPMineViewController ()<WPMineThirdViewDelegate,WPMineSecondViewDelegate,WPMineScrollViewDelegate,UIViewControllerTransitioningDelegate,WPMineLoginDelegate>

@property(nonatomic,weak)WPMineScrollView *scrollView;

@end

@implementation WPMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.hidden = YES;
    
    WPMineScrollView *scrollView = [[WPMineScrollView alloc] initWithFrame:self.view.bounds];
    
    scrollView.secondView.delegate = self;
    scrollView.thirdView.delegate = self;
    scrollView.WPMineDelegate = self;
    
    self.scrollView = scrollView;
    
    scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:scrollView];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self loadUserInfomation];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self setScrollViewContentSize];
}

-(void)setScrollViewContentSize
{
    CGFloat consizeHeight = 0;
    for (UIView *subView in self.scrollView.subviews) {
        consizeHeight += subView.bounds.size.height;
    }
    self.scrollView.contentSize = CGSizeMake(0, consizeHeight+7);
    
}


#pragma mark - 代理方法
-(void)WPMineSecondViewDelegate:(WPMineThirdView *)mineThirdView mineClickType:(WPMineBtnTypes)MineType
{
//    先判断一下用户是否登录
    if ([WPUserAccountViewModel sharedInstance].userLogin)
    {
        if (MineType == WPMineBtnTypesFirst)
        {
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"WPMedicalRecordsVC" bundle:nil];
            WPMedicalRecordsVC *mrVC = [sb instantiateInitialViewController];
            [self.navigationController pushViewController:mrVC animated:YES];
        }
        else if (MineType == WPMineBtnTypesSecond)
        {
            //     体检档案
            WPPhysicalExaminationFileVC *rVC = [WPPhysicalExaminationFileVC new];
            [self.navigationController pushViewController:rVC animated:YES];
        }
        else if (MineType == WPMineBtnTypesThird)
        {
//            我的收藏
            WPMineMyCollectionRecordVC *rVC = [WPMineMyCollectionRecordVC new];
            [self.navigationController pushViewController:rVC animated:YES];
        }
        else if (MineType == WPMineBtnTypesFourth)
        {
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"WPGoldCoinVC" bundle:nil];
            WPGoldCoinVC *gcVC = [sb instantiateInitialViewController];
            [self.navigationController pushViewController:gcVC animated:YES];
        }
        else if(MineType == WPMineBtnTypesFifth)
        {
        //医币提现
             WPYBViewController *gcVC = [WPYBViewController new];
            [self.navigationController pushViewController:gcVC animated:YES];
        }
        else if(MineType == WPMineBtnTypesSixth)
        {

            WPMinePrescriptionRecordsVC *prVC = [WPMinePrescriptionRecordsVC new];
            [self.navigationController pushViewController:prVC animated:YES];
        }
    }
    else
    {
        [[WPUserAccountViewModel sharedInstance] loginTipWith:^{
           
            [self jumpToTheLoginScreen];
            
        }];
    }
}

-(void)WPMineThirdViewDelegate:(WPMineThirdView *)mineThirdView mineClickType:(WPMineBtnTypes)MineType
{
    
    //    先判断一下用户是否登录
    if ([WPUserAccountViewModel sharedInstance].userLogin)
    {
        if(MineType == WPMineBtnTypesFirst)
        {
            WPMineInforamationVC *rVC = [WPMineInforamationVC new];
            [self.navigationController pushViewController:rVC animated:YES];
        }
        else if (MineType == WPMineBtnTypesSecond)
        {
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"WPMineInviteFriendsVC" bundle:nil];
            WPMineInviteFriendsVC *gcVC = [sb instantiateInitialViewController];
            [self.navigationController pushViewController:gcVC animated:YES];
        }
        else if (MineType == WPMineBtnTypesThird)
        {
            WPAboutUsVC *vc = [WPAboutUsVC new];
            WPMineInfomationNVC *pnc = [[WPMineInfomationNVC alloc] initWithRootViewController:vc];
            [self presentViewController:pnc animated:YES completion:nil];
        }
        else if (MineType == WPMineBtnTypesFourth)
        {
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"WPMineFeedbackVC" bundle:nil];
            WPMineFeedbackVC *gcVC = [sb instantiateInitialViewController];
            [self.navigationController pushViewController:gcVC animated:YES];
        }
        else
        {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt:400-633-5851"]];
        }
    }
    else
    {
        [[WPUserAccountViewModel sharedInstance] loginTipWith:^{
            
            [self jumpToTheLoginScreen];
            
        }];
    }
}

-(void)WPMineHeaderViewSettingBtnDidClickWith:(WPMineScrollView *)WPScrollView
{
    if ([WPUserAccountViewModel sharedInstance].userLogin)
    {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"WPSettingViewController" bundle:nil];
        WPSettingViewController *svc = [sb instantiateInitialViewController];
        [self.navigationController pushViewController:svc animated:YES];
    }else
    {
        [[WPUserAccountViewModel sharedInstance] loginTipWith:^{
            
            WPMineLoginVC *vc = [WPMineLoginVC new];
            vc.delegate = self;
            [self presentViewController:[[UINavigationController alloc] initWithRootViewController:vc] animated:YES completion:nil];
            
        }];
    }
}

-(void)WPMineHeaderViewViewLoginBtnDidClickWith:(WPMineScrollView *)WPScrollView
{
    
    if ([[WPUserAccountViewModel sharedInstance] userLogin])
    {
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"WPIndivalInfoNVC" bundle:[NSBundle mainBundle]];
            WPIndivalInfoNVC *nvc = [sb instantiateInitialViewController];
        
            nvc.transitioningDelegate = self;
        
            [self.navigationController presentViewController:nvc animated:YES completion:nil];
    }
    else
    {
        [self jumpToTheLoginScreen];
    }

}

//跳转到登录界面
-(void)jumpToTheLoginScreen
{
    WPMineLoginVC *vc = [WPMineLoginVC new];
    vc.delegate = self;
    [self presentViewController:[[UINavigationController alloc] initWithRootViewController:vc] animated:YES completion:nil];
}

-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    return [[CustomPresentAnimationController alloc] initWithPresenting:YES];
}

-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    return [[CustomPresentAnimationController alloc] initWithPresenting:NO];
}

-(void)loadUserInfomation
{
    WPUserAccount *userAccount = [WPUserAccountViewModel sharedInstance].userAccount;
    if (userAccount)
    {
        if (userAccount.user.image)
        {
            //        从网络加载头像
            [self.scrollView.headerView.iconBtn sd_setImageWithURL:[NSURL URLWithString:userAccount.user.image] placeholderImage:[UIImage imageNamed:@"iconfont-touxiang"]];
        }
        
        self.scrollView.headerView.nickName.text = userAccount.user.name;

    }
    
}

-(void)loadUserInfomation:(WPMineLoginVC *)vc 
{
    WPUserAccount *userAccount = [WPUserAccountViewModel sharedInstance].userAccount;
    if (userAccount.user.image)
    {
        //        从网络加载头像
        
    }
    else
    {
        
    }
    
    self.scrollView.headerView.nickName.text = userAccount.user.name;

}

@end
